home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / vmmstat.lha / VMMStat / RCS / main.c < prev    next >
C/C++ Source or Header  |  1995-07-25  |  25KB  |  1,047 lines

  1. head    1.3;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @ * @;
  6.  
  7.  
  8. 1.3
  9. date    95.07.25.18.02.31;    author enderle;    state Exp;
  10. branches;
  11. next    1.2;
  12.  
  13. 1.2
  14. date    95.07.25.12.49.51;    author enderle;    state Exp;
  15. branches;
  16. next    1.1;
  17.  
  18. 1.1
  19. date    95.07.25.11.29.56;    author enderle;    state Exp;
  20. branches;
  21. next    ;
  22.  
  23.  
  24. desc
  25. @VMMStat displays the statistics of VMM
  26. @
  27.  
  28.  
  29. 1.3
  30. log
  31. @Added workbench startup stuff and improved the
  32. error outputting function (CleanUp()).
  33. @
  34. text
  35. @/*
  36.  *
  37.  *      $VER: main 1.1 (25.07.95) $
  38.  *      $Id: main.c 1.2 1995/07/25 12:49:51 enderle Exp enderle $
  39.  *
  40.  *      $RCSfile: main.c $
  41.  *      $Revision: 1.2 $
  42.  *      $Author: enderle $
  43.  *
  44.  *      (C) Copyright 1995 by Frank Enderle
  45.  *          All Rights Reserved
  46.  *
  47.  *      $Log: main.c $
  48.  * Revision 1.2  1995/07/25  12:49:51  enderle
  49.  * Several minor changes had been made. Extensive comments added.
  50.  * Also some minor bugs have been removed.
  51.  *
  52.  * Revision 1.1  1995/07/25  11:29:56  enderle
  53.  * Initial revision
  54.  *
  55.  *
  56.  */
  57.  
  58. /***** SYSTEM INCLUDES *********************************************/
  59.  
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <exec/memory.h>
  63. #include <libraries/locale.h>
  64. #include <devices/timer.h>
  65.  
  66. #include <clib/dos_protos.h>
  67. #include <clib/exec_protos.h>
  68. #include <clib/locale_protos.h>
  69.  
  70. #ifdef __MAXON__
  71. #include <pragmas/dos_lib.h>
  72. #include <pragmas/exec_lib.h>
  73. #include <pragmas/locale_lib.h>
  74.  
  75. #include <wbstartup.h>
  76. #endif
  77.  
  78. /***** OTHER INCLUDES **********************************************/
  79.  
  80. #include "VMM_Stat.h"                 /* standard include of VMM */
  81. #include "interface.h"                /* interface definitions   */
  82. #include "VMMStatStrings_cat.h"       /* locale definitions      */
  83.  
  84. /***** DEFINES *****************************************************/
  85.  
  86. #define TIMER_SECS        2           /* delay time seconds part */
  87. #define TIMER_MICS        0           /* delay time micros part  */
  88.  
  89. /***** PRIVATE STRUCTURES ******************************************/
  90.  
  91. struct Data {
  92.   struct MsgPort          *timerMP;       /* timer message port  */
  93.   struct timerequest      *timerIO;       /* timerrequest        */
  94.   ULONG                    deviceOpen;    /* TRUE if opened      */
  95.   struct VMStatMsg        *vmStatMsg;     /* message from vmm    */
  96.   ULONG                    vmSignal;      /* vmm signal bit      */
  97. };
  98.  
  99. /***** GLOBAL VARIBALES ********************************************/
  100.  
  101. struct Library *LocaleBase      = NULL;   /* base of locale lib  */
  102. struct Library *MUIMasterBase   = NULL;   /* base of mui lib     */
  103. struct Library *IntuitionBase   = NULL;   /* base of intuition   */
  104. struct Catalog *catalog         = NULL;   /* base of catalog     */
  105. struct ObjApp  *app             = NULL;   /* base of application */
  106.  
  107. static char *verId = "$VER: VMMStat 1.1 (25.07.95) $";
  108.  
  109. /***** EXTERNALS ***************************************************/
  110.  
  111. extern struct Library *SysBase;
  112.  
  113. /***** PROTOTYPES **************************************************/
  114.  
  115. BOOL RequestStats( struct Data * );
  116. void SetupInfo( struct ObjApp *, struct VMStatMsg * );
  117. char *GetString( long );
  118. void TriggerTimer( struct Data *, ULONG, ULONG );
  119. struct Data *OpenAll( void );
  120. void CleanUp( struct Data *, char * );
  121. int main( );
  122.  
  123. /***** SUBROUTINES *************************************************/
  124.  
  125. /************************************************************
  126.  **
  127.  **  NAME
  128.  **    RequestStats - request VMM statistics
  129.  **
  130.  **  SYNOPSIS
  131.  **    BOOL RequestStats( struct Data *data );
  132.  **
  133.  **  FUNCTION
  134.  **    Sends an statistic request to VMM.
  135.  **
  136.  **  INPUTS
  137.  **    struct Data *data:
  138.  **      Pointer to the VMMStat's data structure as returned
  139.  **      by OpenAll()
  140.  **
  141.  **  RESULTS
  142.  **    TRUE
  143.  **      if all is done.
  144.  **
  145.  **    FALSE
  146.  **      if VMM is not installed.
  147.  **
  148.  **  BUGS
  149.  **    Modifies data->vmStatMsg.
  150.  **
  151.  ***********************************************************/
  152.  
  153. BOOL RequestStats( struct Data *data ) {
  154.   struct MsgPort *vmPort;  /* vmm message port */
  155.  
  156.   /* fill the VMStatMsg structure */
  157.  
  158.   data->vmStatMsg->VMMessage.mn_Length = sizeof( struct VMStatMsg );
  159.   data->vmStatMsg->Sender              = FindTask( NULL );
  160.   data->vmStatMsg->Command             = VMCMD_AskStat;
  161.   data->vmStatMsg->ReplySignal         = data->vmSignal;
  162.  
  163.   /* forbid task switching to prevent from changing */
  164.   /* the port lists                                 */
  165.  
  166.   Forbid( );
  167.  
  168.   /* search for vmm port */
  169.  
  170.   if( vmPort = FindPort( "VMM_Port" ) ) {
  171.  
  172.     /* signal vmm by putting a message */
  173.  
  174.     PutMsg( vmPort, (struct Message *)data->vmStatMsg );
  175.  
  176.     /* allow task switching */
  177.  
  178.     Permit( );
  179.  
  180.     /* return TRUE to indicate the success */
  181.  
  182.     return( TRUE );
  183.   } else {
  184.  
  185.     /* allow task switching */
  186.  
  187.     Permit( );
  188.  
  189.     /* return FALSE if VMM isn't installed */
  190.  
  191.     return( FALSE );
  192.   }
  193. }
  194.  
  195. /************************************************************
  196.  **
  197.  **  NAME
  198.  **    SetupInfo - setup information fields
  199.  **
  200.  **  SYNOPSIS
  201.  **    void SetupInfo( struct ObjApp *app,
  202.  **                    struct VMStatMsg *vmStatMsg );
  203.  **
  204.  **  FUNCTION
  205.  **    Fill the information text field of the window. Only
  206.  **    update the contents of a field if they have changed.
  207.  **    This avoids flickering.
  208.  **
  209.  **  INPUTS
  210.  **    struct ObjApp *app:
  211.  **      Pointer to the application GUI structure as returned
  212.  **      by CreateApp(). (Used in function OpenAll())
  213.  **
  214.  **    struct VMStatMsg *vmStatMsg:
  215.  **      Pointer to a filled VMStatMsg structure.
  216.  **
  217.  **  RESULTS
  218.  **    None.
  219.  **
  220.  **  BUGS
  221.  **    May be a bit slowly.
  222.  **
  223.  ***********************************************************/
  224.  
  225. void SetupInfo( struct ObjApp *app, struct VMStatMsg *vmStatMsg ) {
  226.   char  buffer[20];  /* local string buffer */
  227.   char *string;      /* pointer to a string */
  228.  
  229.   /* generate string for 'Free VirtualMem' text field */
  230.  
  231.   sprintf( buffer, "%ld Bytes", vmStatMsg->VMFree );
  232.  
  233.   /* change the field if the contents had changed */
  234.  
  235.   get( app->TX_MAIN_INFO_FREEVIRT, MUIA_Text_Contents, &string );
  236.   if( strcmp( buffer, string) ) {
  237.     set( app->TX_MAIN_INFO_FREEVIRT, MUIA_Text_Contents, buffer );
  238.   }
  239.  
  240.   /* generate string for 'Free FastMem' text field */
  241.  
  242.   sprintf( buffer, "%ld Bytes", AvailMem( MEMF_FAST|MEMF_PUBLIC ) );
  243.  
  244.   /* change the field if the contents had changed */
  245.  
  246.   get( app->TX_MAIN_INFO_FREEFAST, MUIA_Text_Contents, &string );
  247.   if( strcmp( buffer, string) ) {
  248.     set( app->TX_MAIN_INFO_FREEFAST, MUIA_Text_Contents, buffer );
  249.   }
  250.  
  251.   /* generate string for 'Page Faults' text field */
  252.  
  253.   sprintf( buffer, "%ld", vmStatMsg->Faults );
  254.  
  255.   /* change the field if the contents had changed */
  256.  
  257.   get( app->TX_MAIN_INFO_FAULTS, MUIA_Text_Contents, &string );
  258.   if( strcmp( buffer, string) ) {
  259.     set( app->TX_MAIN_INFO_FAULTS, MUIA_Text_Contents, buffer );
  260.   }
  261.  
  262.   /* generate string for 'Written Pages' text field */
  263.  
  264.   sprintf( buffer, "%ld", vmStatMsg->PagesWritten );
  265.  
  266.   /* change the field if the contents had changed */
  267.  
  268.   get( app->TX_MAIN_INFO_PAGESWRITTEN, MUIA_Text_Contents, &string );
  269.   if( strcmp( buffer, string) ) {
  270.     set( app->TX_MAIN_INFO_PAGESWRITTEN, MUIA_Text_Contents, buffer );
  271.   }
  272.  
  273.   /* generate string for 'Read Pages' text field */
  274.  
  275.   sprintf( buffer, "%ld", vmStatMsg->PagesRead );
  276.  
  277.   /* change the field if the contents had changed */
  278.  
  279.   get( app->TX_MAIN_INFO_PAGESREAD, MUIA_Text_Contents, &string );
  280.   if( strcmp( buffer, string) ) {
  281.     set( app->TX_MAIN_INFO_PAGESREAD, MUIA_Text_Contents, buffer );
  282.   }
  283.  
  284.   /* generate string for 'Number Of Frames' text field */
  285.  
  286.   sprintf( buffer, "%ld", vmStatMsg->NumFrames );
  287.  
  288.   /* change the field if the contents had changed */
  289.  
  290.   get( app->TX_MAIN_INFO_NUMFRAMES, MUIA_Text_Contents, &string );
  291.   if( strcmp( buffer, string) ) {
  292.     set( app->TX_MAIN_INFO_NUMFRAMES, MUIA_Text_Contents, buffer );
  293.   }
  294.  
  295.   /* generate string for 'Used Pages' text field */
  296.  
  297.   sprintf( buffer, "%ld", vmStatMsg->PagesUsed );
  298.  
  299.   /* change the field if the contents had changed */
  300.  
  301.   get( app->TX_MAIN_INFO_PAGESUSED, MUIA_Text_Contents, &string );
  302.   if( strcmp( buffer, string) ) {
  303.     set( app->TX_MAIN_INFO_PAGESUSED, MUIA_Text_Contents, buffer );
  304.   }
  305.  
  306.   /* generate string for 'Page Size' text field */
  307.  
  308.   sprintf( buffer, "%ld KByte", (vmStatMsg->PageSize)/1024 );
  309.  
  310.   /* change the field if the contents had changed */
  311.  
  312.   get( app->TX_MAIN_INFO_PAGESIZE, MUIA_Text_Contents, &string );
  313.   if( strcmp( buffer, string) ) {
  314.     set( app->TX_MAIN_INFO_PAGESIZE, MUIA_Text_Contents, buffer );
  315.   }
  316.  
  317.   /* generate string for 'Free TrapStructs' text field */
  318.  
  319.   sprintf( buffer, "%ld", vmStatMsg->TrapStructsFree );
  320.  
  321.   /* change the field if the contents had changed */
  322.  
  323.   get( app->TX_MAIN_INFO_TRAPSTRUCTSFREE, MUIA_Text_Contents, &string );
  324.   if( strcmp( buffer, string) ) {
  325.     set( app->TX_MAIN_INFO_TRAPSTRUCTSFREE, MUIA_Text_Contents, buffer );
  326.   }
  327. }
  328.  
  329. /************************************************************
  330.  **
  331.  **  NAME
  332.  **    GetString - get localized string
  333.  **
  334.  **  SYNOPSIS
  335.  **    char *GetString( long id )
  336.  **
  337.  **  FUNCTION
  338.  **    This function searches the given string id and returns
  339.  **    the string either from the built in catalog (if no
  340.  **    localization is available) or from the locale library.
  341.  **
  342.  **  INPUTS
  343.  **    long id:
  344.  **      string id as defined in VMMStat_Strings.h
  345.  **
  346.  **  RESULTS
  347.  **    A pointer to the (localized) string.
  348.  **
  349.  **  BUGS
  350.  **    none known.
  351.  **
  352.  ***********************************************************/
  353.  
  354. char *GetString( long id ) {
  355.   register long *ccat = (long *)&CatCompArray;  /* built in strings */
  356.  
  357.   if( LocaleBase && catalog ) {
  358.  
  359.     /* get localized string from library and return it */
  360.  
  361.     return( (char *)GetCatalogStr( catalog, id, (STRPTR)*(++ccat) ) );
  362.   } else {
  363.  
  364.     /* search for string in array */
  365.  
  366.     while( id != *ccat ) {
  367.       ccat += 2;
  368.     }
  369.  
  370.     /* return built in string */
  371.  
  372.     return( (char *)*(++ccat) );
  373.   }
  374. }
  375.  
  376. /************************************************************
  377.  **
  378.  **  NAME
  379.  **    TriggerTimer - trigger the timer.device
  380.  **
  381.  **  SYNOPSIS
  382.  **    void TriggerTimer( struct Data *data,
  383.  **                       ULONG secs,
  384.  **                       ULONG mics );
  385.  **
  386.  **  FUNCTION
  387.  **    Perform a timerequest on the timer.device. After
  388.  **    the request has been completed the VMMStat task will
  389.  **    be signaled by the timer.device. This request is
  390.  **    asynchronous.
  391.  **
  392.  **  INPUTS
  393.  **    struct Data *data:
  394.  **      Pointer to a Data structure as returned by OpenAll().
  395.  **
  396.  **    ULONG secs:
  397.  **      Number of seconds to delay.
  398.  **
  399.  **    ULONG mics:
  400.  **      Number of micro seconds to delay.
  401.  **
  402.  **  RESULTS
  403.  **    None.
  404.  **
  405.  **  BUGS
  406.  **    None.
  407.  **
  408.  ***********************************************************/
  409.  
  410. void TriggerTimer( struct Data *data, ULONG secs, ULONG mics ) {
  411.  
  412.   /* fill the timerequest */
  413.  
  414.   data->timerIO->tr_node.io_Command    = TR_ADDREQUEST;
  415.   data->timerIO->tr_time.tv_secs       = secs;
  416.   data->timerIO->tr_time.tv_micro      = mics;
  417.  
  418.   /* and request it */
  419.  
  420.   SendIO( (struct IORequest *)data->timerIO );
  421. }
  422.  
  423. /************************************************************
  424.  **
  425.  **  NAME
  426.  **    OpenAll - open required resources
  427.  **
  428.  **  SYNOPSIS
  429.  **    struct Data *OpenAll( void );
  430.  **
  431.  **  FUNCTION
  432.  **    This subroutine opens all required resources and
  433.  **    allocates/fills the data-structure required in the
  434.  **    whole program. If something goes wrong a direct
  435.  **    call to CleanUp() is performed. In that case OpenAll()
  436.  **    will not return to the caller function.
  437.  **
  438.  **  INPUTS
  439.  **    None.
  440.  **
  441.  **  RESULTS
  442.  **    Pointer to a Data structure.
  443.  **
  444.  **  BUGS
  445.  **    None.
  446.  **
  447.  ***********************************************************/
  448.  
  449. struct Data *OpenAll( void ) {
  450.   struct Data             *data;                                        /* internal data structure */
  451.   static struct TagItem    localeTags[] = { OC_Version, 1, TAG_DONE };  /* tags for locale lib     */
  452.  
  453.   /* check for OS 2 */
  454.  
  455.   if( SysBase->lib_Version < 37 ) {
  456.     CleanUp( NULL, GetString( MSG_MAIN_NOT_OS2 ) );
  457.   }
  458.  
  459.   /* open locale library and catalog file if available */
  460.  
  461.   if( LocaleBase = OpenLibrary( "locale.library", 37 ) ) {
  462.     catalog = OpenCatalogA( NULL, (STRPTR)"vmmstat.catalog", localeTags );
  463.   }
  464.  
  465.   /* allocate buffer for internal data structures */
  466.  
  467.   if( data = AllocVec( sizeof( struct Data ), MEMF_PUBLIC|MEMF_CLEAR ) ) {
  468.  
  469.     /* open intuition.library version 37 or higher */
  470.  
  471.     if( !(IntuitionBase = OpenLibrary( "intuition.library", 37 )) ) {
  472.       CleanUp( data, GetString( MSG_MAIN_NO_INTUITION ) );
  473.     }
  474.  
  475.     /* open muimaster.library version 8 or higher */
  476.  
  477.     if( !(MUIMasterBase = OpenLibrary( "muimaster.library", 8 )) ) {
  478.       CleanUp( data, GetString( MSG_MAIN_NO_MUIMASTER ) );
  479.     }
  480.  
  481.     /* create the application */
  482.  
  483.     if( !(app = CreateApp( )) ) {
  484.       CleanUp( data, GetString( MSG_MAIN_NO_APP ) );
  485.     }
  486.  
  487.     /* create message port for communication with the timer.device */
  488.  
  489.     if( !(data->timerMP = CreateMsgPort( )) ) {
  490.       CleanUp( data, GetString( MSG_MAIN_NO_PORT ) );
  491.     }
  492.  
  493.     /* create io structure for use with the timer.device */
  494.  
  495.     if( !(data->timerIO = (struct timerequest *)CreateExtIO( data->timerMP, sizeof( timerequest ) )) ) {
  496.       CleanUp( data, GetString( MSG_MAIN_NO_MEM ) );
  497.     }
  498.  
  499.     /* open the timer.device */
  500.  
  501.     if( !(data->deviceOpen = !OpenDevice( "timer.device", UNIT_MICROHZ, (struct IORequest *)data->timerIO, 0 )) ) {
  502.       CleanUp( data, GetString( MSG_MAIN_NO_TIMER ) );
  503.     }
  504.  
  505.     /* allocate buffer for the VMStatMsg structure */
  506.  
  507.     if( !(data->vmStatMsg = AllocVec( sizeof( struct VMStatMsg ), MEMF_PUBLIC|MEMF_CLEAR )) ) {
  508.       CleanUp( data, GetString( MSG_MAIN_NO_MEM ) );
  509.     }
  510.  
  511.     /* get a signal for VMM */
  512.  
  513.     if( (data->vmSignal = AllocSignal( -1L )) == -1L ) {
  514.       CleanUp( data, GetString( MSG_MAIN_NO_SIG ) );
  515.     }
  516.  
  517.     /* return pointer */
  518.  
  519.     return( data );
  520.   } else {
  521.  
  522.     /* error: no memory */
  523.  
  524.     CleanUp( NULL, GetString( MSG_MAIN_NO_MEM ) );
  525.   }
  526. }
  527.  
  528. /************************************************************
  529.  **
  530.  **  NAME
  531.  **    CleanUp - free resources
  532.  **
  533.  **  SYNOPSIS
  534.  **    void CleanUp( struct Data *data,
  535.  **                  char *message );
  536.  **
  537.  **  FUNCTION
  538.  **    Free resources allocated by OpenAll() and output a
  539.  **    given message (if there's any). This function never
  540.  **    returns to the caller function.
  541.  **
  542.  **  INPUTS
  543.  **    struct Data *data:
  544.  **      Pointer to a Data structure as returned by OpenAll().
  545.  **
  546.  **    char *message:
  547.  **      Pointer to a message which should be outputed. If
  548.  **      this pointer is NULL nothing will be displayed.
  549.  **      The message can be displayed in three ways:
  550.  **
  551.  **        1) on OS version < 37 a simple Write is performed
  552.  **           to the console.
  553.  **
  554.  **        2) on OS versions >= 37 a requester will popup. If
  555.  **           MUI is available it will be used.
  556.  **
  557.  **        3) on OS versions >= 37 a call from CLI will prompt
  558.  **           messages to the console.
  559.  **
  560.  **  RESULTS
  561.  **    None.
  562.  **
  563.  **  BUGS
  564.  **    None.
  565.  **
  566.  ***********************************************************/
  567.  
  568. void CleanUp( struct Data *data, char *message ) {
  569.   struct Task       *myTask = FindTask( NULL );
  570.   struct EasyStruct  easy   = { sizeof( struct EasyStruct ),
  571.                                 NULL, NULL, NULL, NULL };
  572.  
  573.   /* check for message */
  574.  
  575.   if( message ) {
  576.     if( SysBase->lib_Version < 37 ) {
  577.  
  578.       /* if we run under OS < 37 */
  579.  
  580.       printf( "VMMStat: %s\n", message );
  581.     } else {
  582.  
  583.       /* we run at least on OS 37 */
  584.  
  585.       if( (myTask->tc_Node.ln_Type == NT_PROCESS) && (((struct Process *)myTask)->pr_CLI) ) {
  586.  
  587.         /* if we are a process (from CLI) */
  588.  
  589.         printf( "VMMStat: %s\n", message );
  590.       } else {
  591.  
  592.         /* if we are a task (from Workbench) */
  593.  
  594.         if( MUIMasterBase && app ) {
  595.  
  596.           /* MUI is available and the application has been created */
  597.  
  598.           MUI_Request( app->App, NULL, 0, GetString( MSG_MAIN_ERROR ), GetString( MSG_MAIN_OK ),
  599.                        message, NULL );
  600.         } else {
  601.  
  602.           /* no MUI available so use standard OS 37 EasyRequestArgs */
  603.  
  604.           if( IntuitionBase ) {
  605.  
  606.             /* if intuition is available */
  607.  
  608.             easy.es_Title        = (UBYTE *)GetString( MSG_MAIN_ERROR );
  609.             easy.es_TextFormat   = (UBYTE *)message;
  610.             easy.es_GadgetFormat = (UBYTE *)GetString( MSG_MAIN_OK );
  611.  
  612.             EasyRequestArgs( NULL, (struct EasyStruct *)&easy, NULL, NULL );
  613.           }
  614.         }
  615.       }
  616.     }
  617.   }
  618.  
  619.   /* data structure allocation check */
  620.  
  621.   if( data ) {
  622.  
  623.     /* free the signal */
  624.  
  625.     if( data->vmSignal ) {
  626.       FreeSignal( data->vmSignal );
  627.     }
  628.  
  629.     /* free vmStatMsg memory block */
  630.  
  631.     if( data->vmStatMsg ) {
  632.       FreeVec( data->vmStatMsg );
  633.     }
  634.  
  635.     /* close timer.device */
  636.  
  637.     if( data->deviceOpen ) {
  638.  
  639.       /* empty message queue */
  640.  
  641.       while( GetMsg( data->timerMP ) );
  642.  
  643.       CloseDevice( (struct IORequest *)data->timerIO );
  644.     }
  645.  
  646.     /* free timer.device io structure */
  647.  
  648.     if( data->timerIO ) {
  649.       DeleteExtIO( (struct IORequest *)data->timerIO );
  650.     }
  651.  
  652.     /* remove timer.device message port */
  653.  
  654.     if( data->timerMP ) {
  655.       DeleteMsgPort( data->timerMP );
  656.     }
  657.  
  658.     /* close locale library and catalog if they have been used */
  659.  
  660.     if( LocaleBase ) {
  661.       if( catalog ) {
  662.         CloseCatalog( catalog );
  663.       }
  664.  
  665.       CloseLibrary( LocaleBase );
  666.     }
  667.  
  668.     /* dispose the application */
  669.  
  670.     if( app ) {
  671.       DisposeApp( app );
  672.     }
  673.  
  674.     /* close the muimaster.library */
  675.  
  676.     if( MUIMasterBase ) {
  677.       CloseLibrary( MUIMasterBase );
  678.     }
  679.  
  680.     /* close the intuition.library */
  681.  
  682.     if( IntuitionBase ) {
  683.       CloseLibrary( IntuitionBase );
  684.     }
  685.  
  686.     /* free the data structure */
  687.  
  688.     FreeVec( data );
  689.   }
  690.  
  691.   /* return to the OS */
  692.  
  693.   exit( NULL );
  694. }
  695.  
  696. /***** MAIN PROGRAM ************************************************/
  697.  
  698. int main( ) {
  699.   register struct Data *data;            /* Data structure ptr */
  700.   register LONG         signals;         /* MUI signals        */
  701.   register BOOL         running = TRUE;  /* running indicator  */
  702.  
  703.   /* allocate required resources */
  704.  
  705.   data = OpenAll( );
  706.  
  707.   /* request statistics and exit if VMM is not running */
  708.  
  709.   if( RequestStats( data ) ) {
  710.  
  711.     /* fill text fields */
  712.  
  713.     SetupInfo( app, data->vmStatMsg );
  714.  
  715.     /* open the window */
  716.  
  717.     set( app->WI_MAIN, MUIA_Window_Open, TRUE );
  718.  
  719.     /* trigger the timer */
  720.  
  721.     TriggerTimer( data, TIMER_SECS, TIMER_MICS );
  722.  
  723.     /* main loop */
  724.  
  725.     while( running ) {
  726.       switch( (ULONG)DoMethod( app->App, MUIM_Application_Input, &signals ) ) {
  727.         case MUIV_Application_ReturnID_Quit:
  728.  
  729.           /* application should quit */
  730.  
  731.           /* abort pending timer io's */
  732.  
  733.           AbortIO( (struct IORequest *)data->timerIO );
  734.           WaitIO( (struct IORequest *)data->timerIO );
  735.  
  736.           /* terminate loop */
  737.  
  738.           running = FALSE;
  739.           break;
  740.  
  741.         case ID_BT_MAIN_ABOUT:
  742.  
  743.           /* popup about requester */
  744.  
  745.           MUI_Request( app->App, app->WI_MAIN, 0, GetString( MSG_MAIN_ABOUT ), GetString( MSG_MAIN_OK ),
  746.                        GetString( MSG_MAIN_ABOUT_BODY ), NULL );
  747.           break;
  748.       }
  749.  
  750.       /* signal processing */
  751.  
  752.       if( signals && running ) {
  753.         if( Wait( signals|(1L << data->timerMP->mp_SigBit) ) & (1L << data->timerMP->mp_SigBit) ) {
  754.  
  755.           /* empty message queue */
  756.  
  757.           while( GetMsg( data->timerMP ) );
  758.  
  759.           /* request new statistics */
  760.  
  761.           if( !RequestStats( data ) ) {
  762.  
  763.             /* if not available we quit */
  764.  
  765.             running = FALSE;
  766.           } else {
  767.  
  768.             /* wait for VMM's signal */
  769.  
  770.             Wait( 1L << data->vmSignal );
  771.  
  772.             /* fill text fields */
  773.  
  774.             SetupInfo( app, data->vmStatMsg );
  775.  
  776.             /* trigger the timer */
  777.  
  778.             TriggerTimer( data, TIMER_SECS, TIMER_MICS );
  779.           }
  780.         }
  781.       }
  782.     }
  783.   } else {
  784.  
  785.     /* VMM not found */
  786.  
  787.     CleanUp( data, GetString( MSG_MAIN_NO_VMM ) );
  788.   }
  789.  
  790.   /* a clean exit */
  791.  
  792.   CleanUp( data, NULL );
  793. }
  794. @
  795.  
  796.  
  797. 1.2
  798. log
  799. @Several minor changes had been made. Extensive comments added.
  800. Also some minor bugs have been removed.
  801. @
  802. text
  803. @d3 2
  804. a4 2
  805.  *      $VER: main.c 1.0 (25.07.95) $
  806.  *      $Id: main.c 1.1 1995/07/25 11:29:56 enderle Exp enderle $
  807. d7 1
  808. a7 1
  809.  *      $Revision: 1.1 $
  810. d14 4
  811. d40 2
  812. d69 1
  813. d73 5
  814. a77 1
  815. static char *verId = "$VER: VMMStat 1.0 (25.07.95) $";
  816. d419 6
  817. d435 6
  818. d535 3
  819. d542 41
  820. a582 1
  821.     printf( "VMMStat: %s\n", message );
  822. d646 6
  823. d749 5
  824. @
  825.  
  826.  
  827. 1.1
  828. log
  829. @Initial revision
  830. @
  831. text
  832. @d4 1
  833. a4 1
  834.  *      $Id:$
  835. d6 3
  836. a8 3
  837.  *      $RCSfile$
  838.  *      $Revision$
  839.  *      $Author$
  840. d10 1
  841. a10 1
  842.  *      (C) Copyright 1994 Private Developments
  843. d13 4
  844. a16 1
  845.  *      $Log$
  846. d44 5
  847. d66 1
  848. a66 1
  849. static char *verId = { "$VER: VMMStat 1.0 (25.07.95) $" };
  850. d70 2
  851. a72 4
  852. BOOL RequestStats( struct VMStatMsg * );
  853. void SetupInfo( struct ObjApp *, struct VMStatMsg * );
  854. struct Data *openAll( void );
  855. void cleanUp( struct Data *, char * );
  856. d74 2
  857. d80 27
  858. a106 5
  859. /*
  860. ** Function:     GetString
  861. **
  862. ** Description:  this get's a localized string
  863. */
  864. d108 2
  865. a109 2
  866. char *GetString( long id ) {
  867.   register long *ccat = (long *)&CatCompArray;  /* built in strings */
  868. d111 1
  869. a111 1
  870.   if( LocaleBase && catalog ) {
  871. d113 4
  872. a116 25
  873.     /* get localized string from library and return it */
  874.  
  875.     return( (char *)GetCatalogStr( catalog, id, (STRPTR)*(++ccat) ) );
  876.   } else {
  877.  
  878.     /* search for string in array */
  879.  
  880.     while( id != *ccat ) {
  881.       ccat += 2;
  882.     }
  883.  
  884.     /* return built in string */
  885.  
  886.     return( (char *)*(++ccat) );
  887.   }
  888. }
  889.  
  890. /*
  891. ** Function:     RequestStats
  892. **
  893. ** Description:  requests statistics from VMM
  894. */
  895.  
  896. BOOL RequestStats( struct VMStatMsg *vmStatMsg ) {
  897.   struct MsgPort *vmPort;  /* vmm message port */
  898. d129 1
  899. a129 1
  900.     PutMsg( vmPort, (struct Message *)vmStatMsg );
  901. d150 29
  902. a178 5
  903. /*
  904. ** Function:     SetupInfo
  905. **
  906. ** Description:  setup the various text fields of the window
  907. */
  908. d184 1
  909. a184 1
  910.   /* generate string for text field */
  911. d195 1
  912. a195 1
  913.   /* generate string for text field */
  914. d206 1
  915. a206 1
  916.   /* generate string for text field */
  917. d217 1
  918. a217 1
  919.   /* generate string for text field */
  920. d228 1
  921. a228 1
  922.   /* generate string for text field */
  923. d239 1
  924. a239 1
  925.   /* generate string for text field */
  926. d250 1
  927. a250 1
  928.   /* generate string for text field */
  929. d261 1
  930. a261 1
  931.   /* generate string for text field */
  932. d272 1
  933. a272 1
  934.   /* generate string for text field */
  935. d284 125
  936. a408 3
  937. struct Data *openAll( void ) {
  938.   struct Data             *data;
  939.   static struct TagItem    localeTags[] = { OC_Version, 1, TAG_DONE };
  940. d414 2
  941. d417 3
  942. d421 7
  943. a427 1
  944.       cleanUp( data, GetString( MSG_MAIN_NO_MUIMASTER ) );
  945. d430 2
  946. d433 1
  947. a433 1
  948.       cleanUp( data, GetString( MSG_MAIN_NO_PORT ) );
  949. d436 2
  950. d439 1
  951. a439 1
  952.       cleanUp( data, GetString( MSG_MAIN_NO_MEM ) );
  953. d442 2
  954. d445 1
  955. a445 1
  956.       cleanUp( data, GetString( MSG_MAIN_NO_TIMER ) );
  957. d448 1
  958. a448 3
  959.     if( !(app = CreateApp( )) ) {
  960.       cleanUp( data, GetString( MSG_MAIN_NO_APP ) );
  961.     }
  962. d451 1
  963. a451 1
  964.       cleanUp( data, GetString( MSG_MAIN_NO_MEM ) );
  965. d454 2
  966. d457 1
  967. a457 1
  968.       cleanUp( data, GetString( MSG_MAIN_NO_SIG ) );
  969. d460 2
  970. d464 4
  971. a467 1
  972.     cleanUp( NULL, GetString( MSG_MAIN_NO_MEM ) );
  973. d471 44
  974. a514 1
  975. void cleanUp( struct Data *data, char *message ) {
  976. d519 2
  977. d522 3
  978. d529 2
  979. d535 1
  980. a535 3
  981.     if( app ) {
  982.       DisposeApp( app );
  983.     }
  984. d538 5
  985. d546 2
  986. d552 2
  987. d558 2
  988. d561 4
  989. a564 1
  990.       CloseCatalog( catalog );
  991. d568 8
  992. d580 2
  993. d584 3
  994. d590 6
  995. a595 4
  996. void TriggerTimer( struct Data *data, ULONG secs, ULONG mics ) {
  997.   data->timerIO->tr_node.io_Command    = TR_ADDREQUEST;
  998.   data->timerIO->tr_time.tv_secs       = secs;
  999.   data->timerIO->tr_time.tv_micro      = mics;
  1000. d597 1
  1001. a597 2
  1002.   SendIO( (struct IORequest *)data->timerIO );
  1003. }
  1004. d599 1
  1005. a599 4
  1006. int main( ) {
  1007.   register struct Data    *data;
  1008.   LONG                     signals;
  1009.   BOOL                     running = TRUE;
  1010. d601 1
  1011. a601 1
  1012.   data = openAll( );
  1013. d603 1
  1014. a603 4
  1015.   data->vmStatMsg->VMMessage.mn_Length = sizeof( struct VMStatMsg );
  1016.   data->vmStatMsg->Sender              = FindTask( NULL );
  1017.   data->vmStatMsg->Command             = VMCMD_AskStat;
  1018.   data->vmStatMsg->ReplySignal         = data->vmSignal;
  1019. d605 1
  1020. a605 1
  1021.   TriggerTimer( data, 2, 0 );
  1022. a606 1
  1023.   if( RequestStats( data->vmStatMsg ) ) {
  1024. d609 2
  1025. d613 5
  1026. a617 1
  1027.     SendIO( (struct IORequest *)data->timerIO );
  1028. d621 5
  1029. a626 1
  1030.         case MUIV_Application_ReturnID_Quit:
  1031. d628 4
  1032. d636 3
  1033. d644 2
  1034. d648 11
  1035. a658 2
  1036.           GetMsg( data->timerMP );
  1037.           if( !RequestStats( data->vmStatMsg ) ) {
  1038. d661 3
  1039. d665 3
  1040. d670 3
  1041. a672 1
  1042.             TriggerTimer( data, 2, 0 );
  1043. d679 3
  1044. a681 1
  1045.   cleanUp( data, NULL );
  1046. @
  1047.